package com.ut.demo.slice;

import cn.davidsu.library.ShadowConfig;
import cn.davidsu.library.ShadowHelper;
import cn.davidsu.library.util.LogUtil;
import cn.davidsu.library.util.Util;
import com.ut.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;

public class MainAbilitySlice extends AbilitySlice {

    private TextField etColor;
    private TextField etRadius;
    private TextField etShadowColor;
    private TextField etOffsetX;
    private TextField etOffsetY;
    private Button btnDone;

    private int[] mColor;
    private int mRadius;
    private int mShadowColor;
    private int mOffsetX;
    private int mOffsetY;
    
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mColor = new int[]{0xffffa726};
        mRadius = DisplayUtil.vp2px(this, 30);
        mShadowColor = 0xffffa726;
        mOffsetX = DisplayUtil.vp2px(this, 2);
        mOffsetY = DisplayUtil.vp2px(this, 2);

        bindViews();
        initView();
        setShadow();

    }
    
    private void bindViews() {
        etColor = findComponent(ResourceTable.Id_et_color);
        etRadius = findComponent(ResourceTable.Id_et_radius);
        etShadowColor = findComponent(ResourceTable.Id_et_shadowcolor);
        etOffsetX = findComponent(ResourceTable.Id_et_offsetX);
        etOffsetY = findComponent(ResourceTable.Id_et_offsetY);
        btnDone = findComponent(ResourceTable.Id_btn_done);

        btnDone.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setShadow();
            }
        });

    }

    private void initView() {
        etColor.setText("#ffa726");
        etRadius.setText("30");
        etShadowColor.setText("#ffa726");
        etOffsetX.setText("2");
        etOffsetY.setText("2");
    }

    private void setShadow() {

        initData();

        Text textView1 = findComponent(ResourceTable.Id_tv_1);
        ShadowConfig.Builder config = new ShadowConfig.Builder()
                .setColor(mColor[0])
                .setShadowColor(mShadowColor)
                .setGradientColorArray(mColor)
                .setRadius(mRadius)
                .setOffsetX(mOffsetX)
                .setOffsetY(mOffsetY)
                .setWidth(Util.vp2px(this, 300))
                .setHeight(Util.vp2px(this, 60));
        ShadowHelper.setShadowBgForView(textView1, config);
    }


    private void initData() {
        try {
            if (!DisplayUtil.emptyOrNull(etColor.getText().toString())) {
                String[] colors = etColor.getText().toString().split(",");
                mColor = new int[colors.length];
                for (int i = 0; i < colors.length; i++) {
                    mColor[i] = Util.parseColorFromString(this, colors[i], 0);
                }
            }

            if (!DisplayUtil.emptyOrNull(etShadowColor.getText().toString())) {
                mShadowColor = Util.parseColorFromString(this, etShadowColor.getText().toString(), 0);
            }
        } catch (Exception e) {
            LogUtil.i(e.getLocalizedMessage());
            e.printStackTrace();
            mColor = new int[]{Util.parseColorFromString(this, "#ffa726", 0)};
            mShadowColor = Util.parseColorFromString(this, "#ffa726", 0);
        }

        if (!DisplayUtil.emptyOrNull(etRadius.getText().toString())) {
            mRadius = DisplayUtil.vp2px(this, Integer.valueOf(etRadius.getText().toString()));
        }

        if (!DisplayUtil.emptyOrNull(etOffsetX.getText().toString())) {
            mOffsetX = DisplayUtil.vp2px(this, Integer.valueOf(etOffsetX.getText().toString()));
        }

        if (!DisplayUtil.emptyOrNull(etOffsetY.getText().toString())) {
            mOffsetY = DisplayUtil.vp2px(this, Integer.valueOf(etOffsetY.getText().toString()));
        }
    }
    
    public <T> T findComponent(int id){
        return (T)findComponentById(id);
    }
}
